package com.redrose.videodemo.photo;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.redrose.videodemo.R;
import com.redrose.videodemo.base.BaseActivity;
import com.redrose.videodemo.bean.Photo;
import com.redrose.videodemo.pagetransformer.RotateDownPageTransformer;
import com.redrose.videodemo.utils.GlideUtils;
import com.redrose.videodemo.view.ResizableImageView;

import org.litepal.crud.DataSupport;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

@ContentView(R.layout.activity_bigimage)
public class BigImageActivity extends BaseActivity {
    private int mCurrentMax;
    @ViewInject(R.id.image_viewPager)
    private ViewPager mViewPager;
    private List<Photo> mPhotoList;
    private ImagePagerAdapter mAdapter;
    private int mPosition;
    @ViewInject(R.id.title_layout)
    private LinearLayout mTitleLayout;
    @ViewInject(R.id.bottom_layout)
    private LinearLayout mBottomLayout;
    private boolean isShowExtra = true;
    @ViewInject(R.id.image_position)
    private TextView mPositionView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initAdapter();
        initEvent();
    }

    private void initEvent() {
        mViewPager.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

            }
        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                refrashPosition(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void refrashPosition(int position) {
        position = position + 1;
        String statues = String.format("%d / %d", position, mCurrentMax);
        mPositionView.setText(statues);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isShowExtra) {
            isShowExtra = false;
        } else {
            isShowExtra = true;
        }
        showLayout();
        return false;
    }

    private void showLayout() {
        if (isShowExtra) {
            mTitleLayout.setVisibility(View.VISIBLE);
            mBottomLayout.setVisibility(View.VISIBLE);
        } else {
            mTitleLayout.setVisibility(View.GONE);
            mBottomLayout.setVisibility(View.GONE);
        }
    }

    private void initAdapter() {
        mAdapter = new ImagePagerAdapter();
        mViewPager.setAdapter(mAdapter);
        /** 翻页动画设置 */
        /*mViewPager.setPageTransformer(false,new ZoomOutPageTransformer());*/
        /*mViewPager.setPageTransformer(false,new DepthPageTransformer());*/
        mViewPager.setPageTransformer(false, new RotateDownPageTransformer());
        mViewPager.setCurrentItem(mPosition);
        refrashPosition(mPosition);
    }

    private void initData() {
        Photo specifiefPhoto = (Photo) getIntent().getSerializableExtra("photoData");
        mPhotoList = DataSupport.order("classId desc").find(Photo.class);
        mCurrentMax = mPhotoList.size();
        getRealPosition(specifiefPhoto);
    }

    private void getRealPosition(Photo p) {
        if (p == null) {
            mPosition = 0;
            return;
        }
        int size = mPhotoList.size();
        for (int i = 0; i < size; i++) {
            Photo photo = mPhotoList.get(i);
            if (p.getTime().equals(photo.getTime())) {
                mPosition = i;
                break;
            }
        }
    }

    private class ImagePagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {

            return mCurrentMax;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.image_viewpager_item, null);
            ResizableImageView imageView = view.findViewById(R.id.resize_image);
            GlideUtils.load(imageView, mPhotoList.get(position).getPath());
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
